<?php
require_once ('DataBaseQuery.php');
if ((!isset($_GET["uid"])) || (!isset($_GET["id_f"])))
    header('Location: checklogin.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>AdCo Editor | Collaborative Editor</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="language" content="en" />
        <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="header"><div id="header2">
                <div id="logo">
                    <h1><a href="#">AdCo Editor</a></h1>
                    <p id="subtitle">Online Editor</p>
                </div>
                <div id="menu">
                    <ul>
                        <li><a href="welcome.php">Home</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Services</a></li>
                        <li><a href="#">Gallery</a></li>
                        <li><a href="#">Help</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </div>
            </div></div><!-- header --><!-- header2 -->
        <div id="main"><div id="main2">
                <div id="sidebar">
                    <h2>Login</h2>
                    <?php
                    $user = "";
                    if (!isset($_POST["username"]))
                        $user = $_GET["uid"];
                    else
                        $user = $_POST["username"];
                    echo "<b><font color = " . '"green">' . "Welcome " . $user . "</font></b>";
                    echo "<br><input type=button onClick=" . '"parent.location=' . "'welcome.php'" . '"' . " value=" . '"Logout"><br><br>';
                    ?>
                </div><!-- sidebar -->
                <div id="content">
                    <div class="post">
                        <?php
                        if (isset($_POST["rights"]) && isset($_POST["decision"])) {
                            $id_f = $_GET["id_f"];
                            $uid = $_POST["decision"];
                            if (strcmp($_POST["rights"], "none") == 0)
                                $newRights = -1;
                            else
                                $newRights = $validPermissions[$_POST["rights"]];
                            $ret = setPermissions($id_f, $uid, $newRights);
                            if ($ret == false)
                                echo '<b><font color="red">Rights change failed</font></b>';
                            else
                                echo '<b><font color ="green">Rights changed successfully</font></b>';
                        }
                        ?>
                        <?php
                        $filename = getFileName($_GET["id_f"]);
                        echo "<h2><a href=" . "#>" . $filename . " - Acces Settings</a></h2>"
                        ?>
                        <div class="entry">
                            <p>This page allows you to make changes regarding the access rights over this file.
                                You can consult the list below, which contains the name and rights of other users
                                over this file.
                            </p>
                            <p>
                                You can modify the rights of other users by simply selecting their name from the
                                droplist and choosing the level of access. This option is only available for
                                the owner of this file and other authorized users.
                            </p>
                        </div>
                    </div>
                    <div class="post">
                        <h2><a href="#">Authorized Users List</a></h2>
                        <?php
                        $users = getGlobalFileRights($_GET["id_f"]);
                        while ($u = each($users)) {
                            $username = getUserName($u["key"]);
                            echo "<br/>" . $username . " - " . interpretRights($u["value"]);
                            // TODO consider a table
                        }
                        ?>
                    </div>
                    <div class="post">
                        <h2><a href="#">Modify Access Settings</a></h2>
                        <div class="entry">
                            <?php
                            echo '<form method ="post" action = "settings.php?id_f=' . $_GET["id_f"] . '&uid=' . $_GET["uid"] . '">';
                            ?>
                            <select name="decision">
                                <?php
                                $list = getUsersList();
                                $owner = getUserId($_GET["uid"]);
                                while ($id = each($list)) {
                                    if ($id["key"] != $owner)
                                        echo '<option value="' . $id["key"] . '">' . $id["value"] . '</option>';
                                }
                                ?>
                            </select>
                            <input type = "radio" name = "rights" value = "none">None</input>
                            <input type = "radio" name = "rights" value = "read" checked="checked">Read</input>
                            <input type = "radio" name = "rights" value = "write">Write</input>
                            <input type = "radio" name = "rights" value = "settings">Edit Settings</input>
                            <input type="submit" name="Submit" value="Change"></input>
                            <?php
                            echo '</form>';
                            ?>
                        </div>
                    </div>
                </div><!-- content -->
                <div class="clearing">&nbsp;</div>
            </div></div><!-- main --><!-- main2 -->
        <div id="footer"><div id="footer2">
                <p>Copyright &copy; 2011, designed by <a href="http://www.webtemplateocean.com/">WebTemplateOcean.com</a></p>
            </div></div>
        <div style="text-align: center; font-size: 0.75em;">Design downloaded from <a href="http://www.freewebtemplates.com/">free website templates</a>.</div></body>
</html>